package com.sa.module.uif.controller;

import static com.sa.constant.ModulePathConstant.USER_INFORMATION;
import static com.sa.constant.ModulePathConstant.USER_INFORMATION_SHORT;
import static com.sa.module.uif.constant.SubModulePathConstantUif.LOGIN_REGIST;
import static com.sa.module.uif.constant.SubModulePathConstantUif.LOGIN_REGIST_SHORT;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sa.commons.web.mvc.constant.ViewName;
import com.sa.commons.web.mvc.controller.ControllerSupport;

import com.sa.commons.tools.check.DataValidator;
import com.sa.module.uif.constant.SubModulePathConstantUif;
import com.sa.module.uif.service.LoginRegistService;

@Controller
@RequestMapping(LOGIN_REGIST_SHORT + "/")
public class LoginRegistController extends ControllerSupport {

	@Autowired
	LoginRegistService loginRegistService;

	public LoginRegistController() {
		super(USER_INFORMATION, USER_INFORMATION_SHORT, 
				LOGIN_REGIST, LOGIN_REGIST_SHORT);
	}

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login() throws Exception {
		return createViewName(ViewName.LOGIN);
	}

	@RequestMapping(value = "logout")
	public String logout(Map<String,Object> model) throws Exception {
		loginRegistService.logout(model);
		return createViewName(ViewName.LOGIN);
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(String email, String password, String type,
			Map<String, Object> model) throws Exception {
		
		/**
		 * 数据验证
		 */
		Map<String, String> errors = new HashMap<String, String>();
		
		if (DataValidator.notNull(email) != null) {
			errors.put("email", "邮箱不能为空!");
		}
		if (DataValidator.notNull(password) != null) {
			errors.put("password", "密码不能为空!");
		}
		
		if (!errors.isEmpty()) {
			model.put("errors", errors);
			return createViewName(ViewName.LOGIN);
		}
		
		String error = loginRegistService.login(email, password, model);
		if(error!=null){
			errors.put("email", error);
		}

		if (!errors.isEmpty()) {
			model.put("errors", errors);
			return createViewName(ViewName.LOGIN);
		}
		
		return createHttpExchange("/" + SubModulePathConstantUif.SHOW_MANAGE_SHORT + "/manage", null,Redirect);
	}

	@RequestMapping(value = "reg", method = RequestMethod.GET)
	public String reg() throws Exception {
		return createViewName(ViewName.REG);
	}

	@RequestMapping(value = "reg", method = RequestMethod.POST)
	public String reg(String email, String password, String repassword,  Map<String, Object> model)
			throws Exception {
		/**
		 * 数据验证
		 */
		Map<String, String> errors = new HashMap<String, String>();

		if (DataValidator.notNull(email) != null) {
			errors.put("email", "邮箱不能为空!");
		}
		if (DataValidator.notNull(password) != null) {
			errors.put("password", "密码不能为空!");
		} else {
			if (!password.equals(repassword)) {
				errors.put("password", "密码输入不一致");
			}
		}
		
		if (!errors.isEmpty()) {
			model.put("errors", errors);
			return createViewName("reg");
		}

		loginRegistService.reg(email, password, model);

		return createViewName("reg");
	}

}
